home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / rec_trim.c < prev    next >
C/C++ Source or Header  |  1993-09-10  |  2KB  |  92 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "rec.h"
  25. #include "window.h"
  26. #include "ed_dec.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: rec_trim
  30. |Callby: killer rec_split
  31. |Purpose: Removes some data from within a record.
  32. |Arguments:
  33. |    prec is the record affected.
  34. |    byt1 is the first byte to remove.
  35. |    byt2 is the last byte to remove.
  36. \******************************************************************************/
  37. void rec_trim(prec,byt1,byt2)
  38. rec_ptr *prec;
  39. Int byt1,byt2;
  40. {
  41.     register Int l,i;
  42.     register rec_ptr rec,new,pred;
  43.     register Char *c;
  44.  
  45.     if(byt1 == byt2)
  46.         return;
  47.     rec = *prec;
  48.     new = (rec_ptr)imalloc(sizeof(rec_node));
  49.     l = rec->length - (byt2 - byt1);
  50.     new->data = (Char *)imalloc(l + 1);
  51.     new->recflags = 1;    /* it is a freeable buffer */
  52.     if(l > 0)
  53.     {
  54.         c = new->data;
  55.         if(byt1 > 0)
  56.         {
  57.             memcpy(c,rec->data,byt1);    /* copy first byt1 bytes */
  58.             c += byt1;
  59.         }
  60.         if(byt2 < rec->length)
  61.             memcpy(c,&rec->data[byt2],rec->length - byt2);    /* copy trailing bytes */
  62.     }
  63.     new->data[l] = 0;
  64.     new->length = l;
  65.     pred = rec->prev;
  66.     remq(rec);
  67.     if(rec->recflags & 1)
  68.         ifree(rec->data);
  69.     ifree(rec);
  70.     fixup_recs(rec,new);
  71.     if(rec == SELREC)
  72.     {
  73.         if(SELBYT > byt1 && SELBYT < byt2)
  74.             SELBYT = (!byt1)? 0 : byt1 - 1;
  75.         else if(SELBYT >= byt2)
  76.             SELBYT -= byt2 - byt1;
  77.         SELREC = new;
  78.     }
  79.     for(i = 0;i < NMARK;i++)
  80.         if(MARKREC[i] == rec)
  81.         {
  82.             if(MARKBYT[i] >= byt1 && MARKBYT[i] < byt2)
  83.                 MARKBYT[i] = (!byt1)? 0 : byt1 - 1;
  84.             else if(MARKBYT[i] >= byt2)
  85.                 MARKBYT[i] -= byt2 - byt1;
  86.             MARKREC[i] = new;
  87.         }
  88.     insq(new,pred);
  89.     *prec = new;
  90. }
  91.  
  92.